package com.hmy.finance.bo.response;

import com.hmy.ccp.common.response.BaseResponse;
import com.hmy.finance.common.utils.BigDecimalUtil;
import com.hmy.finance.dal.dos.ReceivablesSumQueryDo;
import com.hmy.finance.dal.dos.ReceiveAdjustSumQueryDo;
import java.math.BigDecimal;
import java.util.Objects;
import lombok.Data;

/**
 * 对账管理-总营收相关汇总字段出参
 */
@Data
public class ReceivablesSumQueryResponse extends BaseResponse {

    private static final long serialVersionUID = -6226815232611026285L;

    /**
     * 总营收
     */
    private String totalRevenueAmount;

    /**
     * 已回款总额
     */
    private String totalReceivedAmount;

    /**
     * 已对账总额
     */
    private String totalReconciliationedAmount;

    /**
     * 营收调整总额
     */
    private String totalAdjustAmount;

    /**
     * 欠款总额
     */
    private String totalArrearsAmount;

    public static ReceivablesSumQueryResponse convertToRsp(ReceivablesSumQueryDo receivablesDo, ReceiveAdjustSumQueryDo adjustDo) {
        ReceivablesSumQueryResponse response = new ReceivablesSumQueryResponse();
        //总营收
        response.setTotalRevenueAmount(Objects.isNull(receivablesDo) ? "0" : BigDecimalUtil.setScale(receivablesDo.getTotalRevenueAmount()).toPlainString());
        //已对账
        response.setTotalReconciliationedAmount(Objects.isNull(receivablesDo) ? "0" : BigDecimalUtil.setScale(receivablesDo.getTotalReconciliationedAmount().compareTo(receivablesDo.getHistoryReconciliationedAmount()) > 0 ? receivablesDo.getTotalReconciliationedAmount() : receivablesDo.getHistoryReconciliationedAmount()).toPlainString());
        //调整金额
        response.setTotalAdjustAmount(Objects.isNull(adjustDo) ? "0" : BigDecimalUtil.setScale(adjustDo.getTotalAdjustAmount()).toPlainString());
        //已回款
        response.setTotalReceivedAmount(Objects.isNull(receivablesDo) ? "0" : BigDecimalUtil.setScale(receivablesDo.getTotalReceivedAmount()).toPlainString());
        //欠款 = 总营收 - 总回款
        response.setTotalArrearsAmount(BigDecimalUtil.setScale((new BigDecimal(response.getTotalRevenueAmount()).subtract(new BigDecimal(response.getTotalReceivedAmount())))).toPlainString());
        
        return response;
    }

}
